package org.example;

import java.sql.SQLException;

public class ForeignKeyService {
    private DatabaseManager databaseManager;

    public ForeignKeyService(DatabaseManager databaseManager) {
        this.databaseManager = databaseManager;
    }

    /**
     * 处理外键值转换
     * @param rule 映射规则
     * @param sourceValue 源字段值
     * @return 处理后的目标值
     * @throws SQLException
     */
    public Object processForeignKey(MappingRule rule, Object sourceValue) throws SQLException {
        if (rule == null || sourceValue == null) {
            return sourceValue;
        }

        String foreignKeyTable = rule.getForeignKeyTable();
        String foreignKeyColumn = rule.getForeignKeyColumn();
        String foreignKeyTargetColumn = rule.getForeignKeyTargetColumn();

        if (foreignKeyTable == null || foreignKeyColumn == null || foreignKeyTargetColumn == null) {
            return sourceValue;
        }

        return databaseManager.queryForeignKeyValue(foreignKeyTable, foreignKeyColumn, foreignKeyTargetColumn, sourceValue);
    }
}